<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const posts = [
            { title: 'I Love JavaScript', author: 'Cavan', id: 1 },
            { title: 'CSS world', author: 'dalao', id: 2 },
            { title: 'CSS', author: 'xiaolao', id: 3 }
        ];
        // posts.push({ title: 'CSS', author: 'xiaolao', id: 3 });
        // localStorage.setItem('posts', JSON.stringify(posts));

        const authors = [
            { name: 'Cavan', hometown: '萍乡', bio: '面向米哈游编程' }
        ];

        function getPostById(id) {
            // 根据 id 找到 post
            // const post = posts.find(post => post.id === id);
            // const { author } = post;
            // console.log(authors.find(item => item.name == author));
            // return post;
            return new Promise((resolve, reject) => {
                console.log('耗时任务前')
                setTimeout(() => {
                    const post = posts.find(post => post.id === id);
                    // console.log(post);
                    if (post) {
                        resolve(post);
                    }
                    else {
                        let err = new Error('匹配失败');
                        reject(err);
                    }
                }, 200)
            })
        };
        getPostById(10)
            .then((data) => {
                console.log(data, 'then时');
            })
            .catch((err) => {
                console.log(err.message);
            });
        getPostById(1)
            .then((data) => {
                console.log(data, 'then时');
            })
            .catch((err) => {
                console.log(err.message);
            });
    </script>
</body>

</html>